# class base for controller
from google.appengine.ext import webapp

# needed for display
from tools import contact_manager

from model import Contact

class Index(webapp.RequestHandler):
    '''
    '''
    def get(self):
        '''
        '''
        # get the language id
        locale_id = self.request.get('language')
        # get the size name
        size = self.request.get('size')

        #
        co_manager = contact_manager.ContactManager(
                locale_id=locale_id, size=size)

        # get the local contact information
        query = Contact().all()
        query.filter('language =', co_manager.language)
        contact = query.get()

        # already set
        if contact:
            contact_key = contact.key()

        # create the minimum requirement
        else:
            #
            contact_key = co_manager.new()

        # display the author to show
        template = co_manager.show(contact_key)
        return self.response.out.write(template)

    def post(self):
        '''
        '''
        pass

class Edit(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size key to use
        size_key = self.request.get('size_key')

        #
        co_manager = contact_manager.ContactManager(
                language_key=language_key, size_key=size_key)

        # retrieve the key
        contact_key = self.request.get('contact_key')

        if not self.request.get('new') == '':
            #
            contact_key = co_manager.new()

        if not self.request.get('edit') == '':
            pass

        # display the author to edit
        template = co_manager.edit(contact_key)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size key to use
        size_key = self.request.get('size_key')

        #
        co_manager = contact_manager.ContactManager(
                language_key=language_key, size_key=size_key)

        # retrieve the key
        contact_key = self.request.get('contact_key')

        # delete
        if not self.request.get('delete') == '':
            pass

        # cancel
        elif not self.request.get('cancel') == '':
            pass

        # we save and display the author
        else:
            # get info
            info = self.request.get('info')

            # tools save the author
            co_manager.save(contact_key, info)

        # display the author to edit
        template = co_manager.show(contact_key)
        return self.response.out.write(template)
